package org.openfidias.jdbase.model;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.openfidias.jdbase.db.DbManager;
import org.openfidias.jdbase.locale.Message;
import org.openfidias.jdbase.template.IModel;
import org.openfidias.jdbase.util.Do;

/**
 *
 * @author atila
 */
public class AlunoDisciplinaDao implements IModel {

   private static final String table = "aluno_disciplina.table";
   public static final String logicalTable = "Aluno_Disciplina";
   private static final String deletedTable = "aluno_disciplina.deleted";

   private AlunoDisciplinaDao() {
   }

   public static AlunoDisciplinaDao getInstance() {
      return AlunoDisciplinaDaoHolder.INSTANCE;
   }

   private static class AlunoDisciplinaDaoHolder {

      private static final AlunoDisciplinaDao INSTANCE = new AlunoDisciplinaDao();
   }

   public void create(AlunoDisciplina alunoDisciplina) throws IOException, Exception {
      try {
         Do.create(alunoDisciplina, table, deletedTable);
      } catch (Exception e) {
         throw new Exception(e);
      }
   }

   public AlunoDisciplina read(String login, int codigo) throws IOException, Exception {
      try {
         AlunoDisciplina ad = new AlunoDisciplina(login, codigo);
         return (AlunoDisciplina) Do.read(ad, table);
      } catch (Exception e) {
         throw new Exception(e);
      }
   }

   public boolean delete(String login, int codigo) throws IOException, Exception {
      try {
         AlunoDisciplina tmpAd = new AlunoDisciplina(login, codigo);
         if (exists(login, codigo)) {
            return Do.delete(tmpAd, table, deletedTable);
         } else {
            // not exists
            System.out.println(Message.notFound("Login e Código"));
            return false;
         }
      } catch (IOException iOException) {
         throw new IOException(Message.tableNotFound(table));
      } catch (Exception e) {
         throw new Exception(e);
      }
   }

   public boolean exists(String login, int codigo) throws IOException, Exception {
      AlunoDisciplina ad = new AlunoDisciplina(login, codigo);
      return Do.exists(ad, table);
   }

   public void list() throws IOException, Exception {
      
   }

   private List<AlunoDisciplina> findAll(String login, int codigo) {

      List<AlunoDisciplina> list = null;
      AlunoDisciplina ad = new AlunoDisciplina(login, codigo);
      try {
         File f = DbManager.getTable(table);
         BufferedReader reader = new BufferedReader(new FileReader(f));
         String line = reader.readLine();
         list = new ArrayList<AlunoDisciplina>();
         while (line != null) {
            if (ad.equalsAtLeastOne(line)) {
               
            }
         }
      } catch (Exception e) {
      }
      
      return list;
   }
}
